﻿/*****************************************************************************
 * Hyldahl.Services.TaskProcessing.WindowsService Created by: MSH 2008.07.22
 * --------------------------------------------------------------------------
 * FileName: ProcessingWindowsService.cs
 * --------------------------------------------------------------------------
 * Copyright 2008 Martin Sixhøj Hyldahl
 *****************************************************************************/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;

namespace Hyldahl.Services.TaskProcessing.WindowsService
{
    public partial class ProcessingWindowsService : ServiceBase
    {
        /*****************************************************
         * FIELDS
         *****************************************************/
        private TaskProcessingController controller;

        /*****************************************************
         * CONSTRUCTOR
         *****************************************************/

        /// <summary>
        /// Initializes a new instance of the <see cref="ProcessingWindowsService"/> class.
        /// </summary>
        public ProcessingWindowsService()
        {
            InitializeComponent();
        }

        /*****************************************************
         * METHODS
         *****************************************************/

        /// <summary>
        /// When implemented in a derived class, executes when a Start command is sent to the service by the Service Control Manager (SCM) or when the operating system starts (for a service that starts automatically). Specifies actions to take when the service starts.
        /// </summary>
        /// <param name="args">Data passed by the start command.</param>
        protected override void OnStart(string[] args)
        {
            controller = new TaskProcessingController(TaskProcessingApplicationContext.Current);
            controller.StartWorkers();
        }

        /// <summary>
        /// When implemented in a derived class, executes when a Stop command is sent to the service by the Service Control Manager (SCM). Specifies actions to take when a service stops running.
        /// </summary>
        protected override void OnStop()
        {
            if (controller != null)
            {
                controller.StopWorkers();

                controller.WaitForCompletion();
            }
        }
    }
}
